package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/abnormalLog")
public class WriteAbnormalLogController extends BaseController{
	@Autowired
	private BizService bizService;

	private static Logger logger = LoggerFactory.getLogger(WriteAbnormalLogController.class);

	/**
	 * 记录异常情况（日志）
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/writeAbnormalLog", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String writeAbnormalLog(HttpServletRequest request)
			throws UnsupportedEncodingException {
		
		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		
		try {
			String Json = request.getParameter("jsons");
			if(Json != null && !Json.isEmpty()){
				String[] jsons = request.getParameter("jsons").split(";");
				List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
	            for(int i = 0;i < jsons.length;i++){
	            	 Map<String, Object> map = JSON.parseObject(jsons[i]);
	            	 listMap.add(map);
	            } 
				//批量插入
				response = bizService.writeAbnormalLog(listMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("必填参数为空");
			}	
		} catch (Exception e) {
			logger.info("writeAbnormalLog:"+ e.toString());
			response.setReturnMsg(1,"写入日志记录失败！");
		} finally {
			return response.toJSON();
		}

	}

}
